<template>
  <div>
    <ZgNewPage
      ref="page"
      title="发起结算"
      :conditions="conditions"
      @condition="onSearch"
      :openCondition="true"
      :pageOpt="pageOpt"
      :dataBtns="dataBtns"
      :adminWidth="140"
      :labelCol="7"
      @search="onSearch"
      @admin="btnClick"
    >
      <template slot="columns">
        <a-table-column
          v-for="(h, i) in headers"
          :key="`column${i}`"
          :width="h.width === 0 ? 'auto' : h.width"
          :title="h.label"
        >
          <template slot-scope="record">
            {{ record[h.code] }}
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
    <ZgDrawer ref="zgDrawer" width="100%" v-model="visible">
      <HandleIndex @close="onClose" :flowInfo="flowInfo"></HandleIndex>
    </ZgDrawer>
  </div>
</template>

<script>
import HandleIndex from '@/views/autoflow/handle/Index'
import { pageMixin } from '@/core/page-mixin'

export default {
  mixins: [pageMixin],
  components: {
    HandleIndex
  },
  data () {
    return {
      pageCode: 'flowHandle-startFlow',
      flowInfo: null,
      pageOpt: {
        showConditionBtn: false
      },
      headers: [
        { code: 'flowName', label: '流程名称', width: 240, isShow: true, sort: 0 },
        { code: 'flowDesc', label: '流程描述', width: 0, isShow: true, sort: 0 }
      ],
      conditions: [
        { code: 'flowName', compType: '单行文本', label: '流程名称', name: '流程名称', defaultVal: '' },
        { code: 'flowCode', compType: '单行文本', label: '流程编码', name: '流程编码', defaultVal: '' }
      ],
      dataBtns: [
        {
          key: 1,
          name: '启动流程',
          code: '',
          type: 'primary',
          icon: 'fa-plane',
          isShow: () => {
            return true
          }
        }
      ],
      visible: false
    }
  },
  methods: {
    // 查询
    onSearch (param) {
      // 加载表头
      console.log(param, 'param')
      this.$http.post('/zg-autoflow/flow/settleWorkflow/getCanRunWorkflowPage', param, (res) => {
        console.log(res, 'res')
        this.$refs.page.endSearch({
          total: Number(res.total),
          list: res.records
        })

        // 填充场景数据
        this.SET_CONTEXT({
          flag: this.pageCode,
          // 数据列表
          dataList: res.records.map((t, i) => {
            return {
              index: i + 1,
              id: t.id
            }
          }),
          // 筛选条件
          condition: this.setCondition(this.conditions),
          // 按钮操作
          operate: this.operatesArr({ dataBtns: this.dataBtns }),
          // 视图字段
          form: []
        })
      })
    },
    btnClick (val) {
      if (val.key === 1) {
        this.$http.post('/zg-autoflow/flow/workflowManage/startWorkflow', { flowCode: val.obj.flowCode }, (res) => {
          this.$set(this, 'flowInfo', {
            instCode: res.FlowInsCode,
            stepInstCode: res.StepInsCode
          })
          this.$refs.zgDrawer.open()
        })
      }
    },
    onClose () {
      this.visible = false
    }
  },
  mounted () {
    this.$eventBus.$off('order')
    this.$eventBus.$on('order', (msg) => {
      if (msg.flag === this.pageCode) {
        if (msg.order === 'dataList') {
          this.$refs.page.setPageIds(msg.dataList.map(x => {
            return x.id
          }))
        }
      }
    })
  }
}
</script>

<style scoped>
</style>
